<template>
  <div class="list">
    <!-- 第一行选项卡 -->
    <el-tabs v-model="activeName" @tab-click="handleClick">
      <el-tab-pane label="所有订单" name="所有订单" />
      <el-tab-pane label="待支付" name="待支付" />
      <el-tab-pane label="待派单" name="待派单" />
      <el-tab-pane label="待接单" name="待接单" />
      <el-tab-pane label="待服务" name="待服务" />
      <el-tab-pane label="待确认" name="待确认" />
      <el-tab-pane label="已完成" name="已完成" />
    </el-tabs>
    <!-- 第二行表格 -->
    <el-table :data="tableData" stripe style="width: 100%">
      <el-table-column prop="id" label="订单编号" align="center" width="100" />
      <el-table-column
        prop="orderTime"
        label="下单时间"
        width="180"
        align="center"
      >
        <template slot-scope="scope">
          {{ scope.row.orderTime | parseTime }}
        </template>
      </el-table-column>
      <el-table-column prop="total" label="总价" width="180" align="center">
        <template slot-scope="scope">
          <div class="price">
            ￥<span>{{ scope.row.total }}</span
            >元
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="status" label="状态" align="center">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status === '待支付'" type="danger"
            >待支付</el-tag
          >
          <el-tag v-if="scope.row.status === '待派单'">待派单</el-tag>
          <el-tag v-if="scope.row.status === '待接单'" type="warning"
            >待接单</el-tag
          >
          <el-tag v-if="scope.row.status === '待服务'" type="info"
            >待服务</el-tag
          >
          <el-tag v-if="scope.row.status === '待确认'" type="info"
            >待确认</el-tag
          >
          <el-tag v-if="scope.row.status === '已完成'" type="success"
            >已完成</el-tag
          >
        </template>
      </el-table-column>
      <el-table-column
        prop="customerId"
        label="顾客ID"
        width="180"
        align="center"
      />
      <el-table-column
        prop="employeeId"
        label="员工ID"
        width="180"
        align="center"
      />
      <el-table-column label="操作" width="180" align="center">
        <template slot-scope="scope">
          <a href="#" @click.prevent="toDetails(scope.row.id)">详情</a>
          <a
            v-if="scope.row.status === '待接单'"
            href="#"
            class="quxiao"
            @click.prevent="cancleDan(scope.row.id)"
            >取消派单</a
          >
          <a
            v-if="scope.row.status === '待派单'"
            href="#"
            class="paidan"
            @click.prevent="paiDan(scope.row.id)"
            >派单</a
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 第三行分页组件 -->
    <el-pagination
      :current-page="params.page"
      :page-sizes="[2, 5, 10, 20]"
      :page-size="params.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />

    <!-- 派单模态框 -->
    <el-dialog title="派单" :visible.sync="visible">
      <el-radio
        v-for="item in waiters"
        :key="item.id"
        v-model="paramsP.waiterId"
        :label="item.id"
        >{{ item.username }}{{ item.realname }}</el-radio
      >
      <div slot="footer" class="dialog-footer">
        <el-button @click="visible = false">取 消</el-button>
        <el-button type="primary" @click="toPaiDan">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { queryOder, getWaiters, paiDans, cancelOrder } from "@/api/order";
export default {
  data() {
    return {
      activeName: "所有订单",
      tableData: [],
      // 查询订单的参数
      params: {
        page: 1,
        pageSize: 10,
      },
      // 查询员工的参数
      waiterP: {
        page: 1,
        pageSize: 100,
        roleId: 1,
      },
      // 数据的总条数
      total: 0,
      // 控制派单模态框显示与隐藏
      visible: false,

      // 员工们
      waiters: [],
      // 派单参数，
      paramsP: {},
    };
  },
  created() {
    this.getOrders();
  },
  methods: {
    handleClick(tab, event) {
      // console.log(tab, event);
      // 默认跳转到第一页
      this.params.page = 1;
      // 根据不同的类别查询不同的订单数据
      if (this.activeName !== "所有订单") {
        this.params.status = this.activeName;
      } else {
        delete this.params.status;
      }
      // 重新请求数据
      this.getOrders();
    },
    // 根据条件分页查询订单
    async getOrders() {
      const res = await queryOder(this.params);
      this.tableData = res.data.list;
      this.total = res.data.total;
      // console.log(res.data.list);
    },
    // 当前页的数量发生改变时
    handleSizeChange(pageSize) {
      this.params.pageSize = pageSize;
      // 每次当前页的数量发生改变，默认跳转到第一页
      this.params.page = 1;
      // 重新请求数据
      this.getOrders();
    },
    // 当前页码发生改变时
    handleCurrentChange(page) {
      this.params.page = page;
      // 重新请求数据
      this.getOrders();
    },
    // 获取所有员工信息
    async getAllWaiter() {
      const res = await getWaiters(this.waiterP);
      this.waiters = res.data.list;
    },
    // 确认派单
    async toPaiDan() {
      // 确认派单
      const res = await paiDans(this.paramsP);
      if (res.status === 200) {
        this.$message({
          type: "success",
          message: "派单成功",
        });
        // 关闭模态框
        this.visible = false;
        // 重新获取数据
        this.getOrders();
      } else {
        this.$message.error({
          message: "派单失败",
        });
        // 关闭模态框
        this.visible;
      }
    },
    // 派单
    paiDan(id) {
      // 获取所有员工数据
      this.getAllWaiter();
      // 获取当前行的订单id
      this.paramsP.orderId = id;
      // 显示模态框
      this.visible = true;
    },
    // 取消派单
    cancleDan(orderId) {
      this.$confirm("此操作将取消派单，是否确认?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          const res = await cancelOrder({ orderId });
          console.log(res);
          if (res.status === 200) {
            this.$message({
              type: "success",
              message: "操作成功!",
            });
            // 重新获取数据
            this.getOrders();
          } else {
            this.$message.error({
              message: "操作失败!",
            });
          }
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    // 跳转到订单详情页
    toDetails(id) {
      this.$router.push({
        path: "/admin/order/details",
        query: {
          index: id,
        },
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.price {
  font-size: 16px;
  span {
    color: #e65d6e;
  }
}

// 取消派单
.quxiao {
  color: #ffba00;
}
// 派单
.paidan {
  color: #30b08f;
}
a {
  margin: 0 5px;
}
</style>
